package com.chenzouquan.util.verify;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;

public class XMLValidator {
    public static boolean validateXMLSyntax(String filePath) {
        try {
            // 创建DocumentBuilder对象
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            // 解析XML文件
            Document document = builder.parse(new File(filePath));
            // 如果解析过程中没有抛出异常，则XML语法是有效的
            return true;
        } catch (SAXException | IOException e) {
            // 处理解析错误
            System.out.println("语法错误"+e.getMessage());
            return false;
        } catch (Exception e) {
            // 处理其他错误
            System.out.println("语法错误"+e.getMessage());
            return false;
        }
    }
}

